<?php
/**
 * Created by PhpStorm.
 * User: yearnfar<207736107@qq.com>
 * Date: 2015/4/27 0027
 * Time: 下午 6:47
 */

namespace Yearnfar\Bundle\CmsBundle\Controller;


use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Yearnfar\Bundle\CmsBundle\Entity\calendar;

/**
 * @Route("/admin")
 * @Template()
 */
class AdminController extends BaseController{

    /**
     * @Route("/")
     */
    public function indexAction()
    {
        /*$u = $this->getUser();
        print_r($u);
        exit();*/
        return [];
    }

    /**
     * @Route("/calendar/{oper}")
     */
    public function calendarAction($oper)
    {
        $request = $this->get('request');

        $id    = $request->get('id',     0);
        $title = $request->get('title', '');
        $start = $request->get('start',  0);
        $end   = $request->get('end',    0);
        $allDay= $request->get('allDay', 1);
        $class = $request->get('class', '');

        $em   = $this->getDoctrine()->getManager();
        $uid  = $this->getUser()->getId();

        switch ($oper) {
            //
            case 'fetch':
                $rows = $em
                    ->createQuery("SELECT c FROM CmsBundle:calendar c WHERE c.uid ={$uid} and c.start >={$start} and c.end<{$end}")
                    ->getArrayResult();
                //
                if ($rows) {
                    foreach ($rows as $key => $val) {
                        $rows[$key]['start'] = date('Y-m-d H:i', $val['start']);
                        $rows[$key]['end']   = date('Y-m-d H:i', $val['end']);
                    }
                } else {
                    $rows = [];
                }
                return $this->jsonOut($rows, false);
            //
            case 'add':
                $o = new calendar();
                $o
                    ->setUid($uid)
                    ->setTitle($title)
                    ->setStart($start)
                    ->setEnd($end)
                    ->setAllday($allDay)
                    ->setClass($class);
                //
                $em->persist($o);
                $em->flush();
                break;
            //
            case 'edit' :
                $o = $em->find('CmsBundle:calendar', $id);
                if ($o && $o->getUid()==$uid) {
                    $o
                        ->setTitle($title)
                        ->setStart($start)
                        ->setEnd($end)
                        ->setAllDay($allDay);

                    $em->persist($o);
                    $em->flush();
                }
                break;
            //
            case 'del'  :
                $o = $em->find('CmsBundle:calendar', $id);
                if ($o  && $o->getUid()==$uid) {
                    $em->remove($o);
                    $em->flush();
                }
                break;
        }

        return $this->jsonOut([]);
    }

    /**
     * @Route("/login")
     */
    public function loginAction()
    {
        return [];
    }

    /**
     * @Route("/login/check")
     */
    public function loginCheckAction()
    {
        return [];
    }

    /**
     * @Route("/logout")
     */
    public function logoutAction()
    {
        return [];
    }

    /**
     * @Route("/forgot")
     */
    public function forgotAction()
    {
        return [];
    }

    /**
     * @Route("/register")
     */
    public function registerAction()
    {
        return [];
    }

} 